package com.zmsoft.boxy.sfg.service;

import org.cnsoft.framework.beans.common.RESTResultBean;
import org.cnsoft.framework.beans.user.UserBean;
import org.cnsoft.framework.utils.EmptyHelper;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.zmsoft.boxy.sfg.player.BoxySFGPlayer;
import com.zmsoft.persistent.sfg.Sfg661010PlayerFarm.Sfg661010PlayerFarmDBO;
import com.zmsoft.persistent.sfg.Sfg661021PlayerLand.Sfg661021PlayerLandDBO;
import com.zmsoft.persistent.sfg.Sfg661040PlayerPocket.Sfg661040PlayerPocketDBO;
import com.zmsoft.persistent.sfg.Sfg662010GameLand.Sfg662010GameLandDBO;
import com.zmsoft.persistent.sfg.Sfg662040GameCrops.Sfg662040GameCropsDBO;
import com.zmsoft.persistent.sfg.Sfg662041GameCropsGrow.Sfg662041GameCropsGrowDBO;

/**
 * 超管登录
 * 
 * @author ZmSoft
 * @version 0.1.0 2018/3/1
 * @since 0.1.0 2018/3/1
 * @see ApiManagerLoginController
 */
@Service("SFGGameActionService") // 替换系统登录接口
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
//@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class SFGGameActionService extends MySFGGameServiceSupport {

	/**
	 * 加载商店信息
	 * @param player
	 * @return
	 * @throws Exception
	 */
	public RESTResultBean<UserBean> loadShop(BoxySFGPlayer player) throws Exception {
		RESTResultBean<UserBean> result = new RESTResultBean<UserBean>();
		result.setData(sfgGameInfoCacheService.loadGameShop(ONE));
		return result;
	}

	/**
	 * 商店购买信息
	 */
	public RESTResultBean<UserBean> doShopBuy(BoxySFGPlayer player) throws Exception {
		// TODO Auto-generated method stub
		return new RESTResultBean<UserBean>();
	}

	/**
	 * 作物种植信息
	 */
	public RESTResultBean<UserBean> doLandPlant(BoxySFGPlayer player) throws Exception {
		RESTResultBean<UserBean> result = new RESTResultBean<UserBean>();
		// 用户农场信息
		Sfg661010PlayerFarmDBO playerFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player);
		// 获得用户土地信息
		Sfg661021PlayerLandDBO playerLand = sfgPlayerInfoCacheService.loadPlayerLand(player);
		// 用户口袋数据
		Sfg661040PlayerPocketDBO playerPocketProp = sfgPlayerInfoCacheService.loadPlayerPocketProp(player);
		// 1未开垦2未种植3成长中5成熟待收割9收割后未翻地)
		if (EmptyHelper.isEmpty(playerFarm) || EmptyHelper.isEmpty(playerLand) || EmptyHelper.isEmpty(playerPocketProp)) {
			result.setCode(1);
			result.setMsg(FAIL_MESSAGE_BUG);
			return result;
		}
		// 合理化检查
		if (TWO.equals(playerLand.getLandState()) == false) {
			result.setCode(1);
			result.setMsg(FAIL_ACTION_sow);
			return result;
		}
		// 获得物品信息
		Sfg662040GameCropsDBO gameCrop = sfgGameInfoCacheService.loadGameCropsData(SFG_GAME_FARM_TYPE, playerPocketProp.getPropId());
		if (EmptyHelper.isEmpty(gameCrop)) {
			result.setCode(1);
			result.setMsg(FAIL_MESSAGE_BUG);
			return result;
		}
		// 检查需要金钱、等级、道具
		{
			if (gameCrop.getNeedMoney() > playerFarm.getCurMoney()) {// 金钱不足
				result.setCode(1);
				result.setMsg(FAIL_MESSAGE_Money);
				return result;
			}
			if (gameCrop.getNeedFarmLevel() > playerFarm.getFarmLevel()) {// 农场等级不足
				result.setCode(1);
				result.setMsg(FAIL_MESSAGE_FarmLevel);
				return result;
			}
			if (gameCrop.getNeedExp() > playerFarm.getTotalExp()) {// 消耗经验
				result.setCode(1);
				result.setMsg(FAIL_MESSAGE_Exp);
				return result;
			}
			{// 时间秒
			}
		}
		// 扣除需要金钱和经验、道具
		{
			playerFarm.setCurMoney(playerFarm.getCurMoney() - gameCrop.getNeedMoney());
			playerFarm.setTotalExp(playerFarm.getTotalExp() - gameCrop.getNeedExp());
			playerLand.setLandState(THREE);
		}
		// 扣除口袋物品
		if(playerPocketProp.getPropNum() > 0){
			playerPocketProp.setPropNum(playerPocketProp.getPropNum() - 1);
		}else{
			result.setCode(1);
			result.setMsg(FAIL_MESSAGE_Pocket);
			return result;
		}
		// 整理植物基本数据到土地下面
		{
			// 作物ID
			playerLand.setCropsId(gameCrop.getId());
			// 作物名称
			playerLand.setCropsName(gameCrop.getCropsName());
			// 作物等级
			playerLand.setCropsLevel(gameCrop.getCropsLevel());
			// 成熟需要时间秒
			playerLand.setCropsNeedTime(gameCrop.getNeedTime());
			// 种植时间-秒
			playerLand.setPlantTime(System.currentTimeMillis());// 开始时间
			// 季数
			playerLand.setSeasonsNum(1L);
		}
		// 整理植物阶段数据到土地下面
		// 1土地种子/2幼苗发芽/3茁壮成长/4长大开花/5成熟结果
		Sfg662041GameCropsGrowDBO gameCropsGrowDBO = sfgGameInfoCacheService.loadGameCropsGrowData(SFG_GAME_FARM_TYPE, playerPocketProp.getPropId(), ONE);
		if (EmptyHelper.isEmpty(gameCropsGrowDBO)) {
			result.setCode(1);
			result.setMsg(FAIL_MESSAGE_BUG);
			return result;
		}
		{
			// 当前阶段ID
			playerLand.setStageId(gameCropsGrowDBO.getId());
			// 当前阶段名称
			playerLand.setStageName(gameCropsGrowDBO.getStageName());
			// 土地图片
			playerLand.setLandPicUrl(gameCropsGrowDBO.getStagePicUrl());
			// 阶段互动ID
			playerLand.setActionId(gameCropsGrowDBO.getActionId());
			// 阶段互动名称
			playerLand.setActionName(gameCropsGrowDBO.getActionName());
			// 当前阶段需要时间(秒) need_time
			playerLand.setNeedTime(gameCropsGrowDBO.getNeedTime());
			playerLand.setCropsNeedTime1(gameCropsGrowDBO.getNeedTime());

			// 剩余浇水数目
			playerLand.setLeftWatering(gameCropsGrowDBO.getLeftWatering());
			// 剩余除草数目
			playerLand.setLeftWeeding(gameCropsGrowDBO.getLeftWeeding());
			// 剩余杀虫数目
			playerLand.setLeftInsect(gameCropsGrowDBO.getLeftInsect());
			// 剩余施肥数目
			playerLand.setLeftFertilize(gameCropsGrowDBO.getLeftFertilize());
			// 可以获得经验数目
			playerLand.setCropsExp(gameCropsGrowDBO.getCropsExp());
			// 动作增加经验
			playerFarm.setTotalExp(playerFarm.getTotalExp() + gameCropsGrowDBO.getCropsExp());
		}

		// 最大果实数目 fruit_max
		// 最大可偷取果实数目 fruit_steal_max
		// 剩余可偷取果实数目 fruit_steal_left
		// 果实单价-分 fruit_price
		// 果实占用仓库容积大小 fruit_volume

		// 用户数据集中保存
		sfgPlayerInfoCacheService.savePlayer(player, playerFarm, playerLand, null, playerPocketProp);

		result.setMsg(ACTION_OK_MESSAGE);
		result.setData(playerLand);// 数据刷新展示

		// 补全各个阶段时间
		prepareLandCropsGrow(player, playerFarm, playerLand);

		return result;
	}

	// 补全各个阶段时间
	@Async("threadPoolTaskExecutor")
	public void prepareLandCropsGrow(BoxySFGPlayer player, Sfg661010PlayerFarmDBO farm, Sfg661021PlayerLandDBO land) throws Exception {
		// 1土地种子
		Sfg662041GameCropsGrowDBO gameCropsGrowDBO;
		// 2幼苗发芽
		gameCropsGrowDBO = sfgGameInfoCacheService.loadGameCropsGrowData(SFG_GAME_FARM_TYPE, land.getCropsId(), TWO);
		if (EmptyHelper.isNotEmpty(gameCropsGrowDBO)) {
			// 当前阶段需要时间(秒) need_time
			land.setCropsNeedTime2(gameCropsGrowDBO.getNeedTime());
		}
		// 3茁壮成长
		gameCropsGrowDBO = sfgGameInfoCacheService.loadGameCropsGrowData(SFG_GAME_FARM_TYPE, land.getCropsId(), THREE);
		if (EmptyHelper.isNotEmpty(gameCropsGrowDBO)) {
			// 当前阶段需要时间(秒) need_time
			land.setCropsNeedTime3(gameCropsGrowDBO.getNeedTime());
		}
		// 4长大开花
		gameCropsGrowDBO = sfgGameInfoCacheService.loadGameCropsGrowData(SFG_GAME_FARM_TYPE, land.getCropsId(), FOUR);
		if (EmptyHelper.isNotEmpty(gameCropsGrowDBO)) {
			// 当前阶段需要时间(秒) need_time
			land.setCropsNeedTime4(gameCropsGrowDBO.getNeedTime());
		}
		// 5成熟结果
		gameCropsGrowDBO = sfgGameInfoCacheService.loadGameCropsGrowData(SFG_GAME_FARM_TYPE, land.getCropsId(), FIVE);
		if (EmptyHelper.isNotEmpty(gameCropsGrowDBO)) {
			// 当前阶段需要时间(秒) need_time
			land.setCropsNeedTime5(gameCropsGrowDBO.getNeedTime());
		}
		// 6果实收割

		//补全土地
		sfgPlayerInfoCacheService.preparePlayerFarmLand(farm);
		
		//保存数据
		sfgPlayerInfoCacheService.savePlayerFarm(farm);
		sfgPlayerInfoCacheService.savePlayerLand(player, land);
	}

	/**
	 * 土地开垦信息(从无到有)[下一步种植]
	 * 
	 * @throws Exception
	 */
	public RESTResultBean<UserBean> doLandReclamation(BoxySFGPlayer player) throws Exception {
		RESTResultBean<UserBean> result = new RESTResultBean<UserBean>();
		// 获得用户土地信息
		Sfg661021PlayerLandDBO playerLand = sfgPlayerInfoCacheService.loadPlayerLand(player);
		// 土地卡、经验值、好友
		// 1未开垦2未种植3成长中5成熟待收割9收割后未翻地)
		if (ONE.equals(playerLand.getLandState()) == false) {
			result.setCode(1);
			result.setMsg(FAIL_ACTION_bring);
			return result;
		}
		// 用户农场信息
		Sfg661010PlayerFarmDBO playerFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player);
		// 获得土地升级配置
		Long nextLand = playerFarm.getLandNumUse() + 1;
		Sfg662010GameLandDBO gameLand = sfgGameInfoCacheService.loadGameLandData(SFG_GAME_FARM_TYPE, nextLand);
		// 检查需要金钱、等级、道具
		{
			if (gameLand.getNeedMoney() > playerFarm.getCurMoney()) {// 金钱不足
				result.setCode(1);
				result.setMsg(FAIL_MESSAGE_Money);
				return result;
			}
			if (gameLand.getNeedFarmLevel() > playerFarm.getFarmLevel()) {// 农场等级不足
				result.setCode(1);
				result.setMsg(FAIL_MESSAGE_FarmLevel);
				return result;
			}
		}
		// 扣除需要金钱和经验、道具
		{
			playerFarm.setCurMoney(playerFarm.getCurMoney() - gameLand.getNeedMoney());
			playerLand.setLandState(TWO);// 更新土地状态
			// 动作增加经验
			playerFarm.setTotalExp(playerFarm.getTotalExp() + 5);// TODO 等待转到配置
		}
		// 用户数据集中保存
		sfgPlayerInfoCacheService.savePlayer(player, playerFarm, playerLand, null, null);
		result.setData(playerLand);// 数据刷新展示
		return result;
	}

	/**
	 * 摘取果实/农场升级信息
	 */
	public RESTResultBean<UserBean> doFarmPick(BoxySFGPlayer player) throws Exception {
		// TODO Auto-generated method stub
		return new RESTResultBean<UserBean>();
	}

}
